package com.byt.framlib.commonutils;

import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 有关金额的输入监听，最多可以输入为999.99
 */
public class MoneyShowLishLister implements TextWatcher {

    private String filterString = "";
    private EditText mEditText;
    //  private Button mButton;
    private Pattern mPattern = Pattern.compile("^0$|^\\d{0,3}$|(^\\d{0,3}\\.\\d{0,2}$)");
    private Pattern pattern = Pattern.compile("^0$|^\\d{0,3}$|(^\\d{0,3}$)");
    private int mType;
    private double money = 0d;

    public MoneyShowLishLister(EditText mEditText/*, Button button*/) {
        super();
        this.mEditText = mEditText;
//    this.mButton = button;
        this.mType = 1;
    }

    public MoneyShowLishLister(EditText mEditText,/* Button button,*/ int type) {
        super();
        this.mEditText = mEditText;
//    this.mButton = button;
        this.mType = type;
    }

    public MoneyShowLishLister(EditText mEditText, /*Button button, */int type, double money) {
        super();
        this.mEditText = mEditText;
//    this.mButton = button;
        this.mType = type;
        this.money = money;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int selPos = start;
        Editable editable = mEditText.getText();
        String content = editable.toString();
        if (filterString.length() > content.length()) {
            selPos++;
        }
        if (filterString.equals(content)) {
            return;
        }
        Matcher matcher = null;
        if (mType == 1) {
            matcher = pattern.matcher(content);
        } else {
            matcher = mPattern.matcher(content);
        }

        if (matcher.find()) {
            if (content.contains(".")) {
                String preDot = content.substring(0, content.indexOf("."));
                if (preDot.length() > 1 && preDot.startsWith("0")) {
                    content = content.substring(1, content.length());
                    selPos = 1;
                } else if (TextUtils.isEmpty(preDot)) {
                    content = "0" + content;
                    selPos = 1;
                }
            } else {
                if (content.length() > 1 && content.startsWith("0")) {
                    content = content.substring(1, content.length());
                    selPos = 1;
                }
            }
        } else if (TextUtils.isEmpty(content)) {
            content = "";
            selPos = 0;
        } else {
            content = filterString;
        }
//
//    if (mButton != null) {
//      if (!TextUtils.isEmpty(content) && !content.endsWith(".")
//          && Double.parseDouble(content) >= 0.01) {
//        mButton.setEnabled(true);
//      } else {
//        mButton.setEnabled(false);
//      }
//    }

        if (filterString.length() < content.length()) {
            selPos++;
        } else if (filterString.length() > content.length()) {
            selPos--;
        }
        if (selPos < 0) {
            selPos = 0;
        } else if (selPos > content.length()) {
            selPos = content.length();
        }
        if (money != 0d && !TextUtils.isEmpty(content)) {
            if (Double.parseDouble(content) - money > 0d) {
                content = MoneyShowTool.twolastDF(money);
            }
        }
        mEditText.setText(content);
        editable = mEditText.getText();
        Selection.setSelection(editable, selPos);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        filterString = s.toString();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
